import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreUploadService extends BaseService<any> {
  private readonly logger = new Logger(CoreUploadService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * image
   * 对应 PHP: CoreUploadService_core::image()
   * 逻辑类型: undefined - undefined
   */
  async image(file: number, site_id: number, file_dir: number, cate_id: number, storage_type: number) {
    // 基于PHP真实逻辑: image
    // PHP原文: //实例化上传引擎         $this->upload_driver = $this->driver($site_id, $storage_type);         //读取上传附件的信息用于后续得校验和数据写入         $this->upload_driver->read($f...
//实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        //生成缩略图....
        return this.after(site_id, file_dir, FileDict.IMAGE, cate_id);
    }

    /**
     * 上传
     * @param int site_id
     * @param string file_dir
     * @param string type
     * @param int cate_id
     * @return */
    async after(int site_id, string file_dir, string type, int cate_id = 0)
    {
        const file_info = this.upload_driver.getFileInfo(];
        const dir = this.root_path  + '/' . file_dir;
        //执行上传
        this.upload_driver.setType(type).setValidate(this.validate).upload(dir];
        const file_name = this.upload_driver.getFileName(];
        const full_path = this.upload_driver.getFullPath(dir];
        const core_attachment_service = this.coreAttachmentService;
        const url = this.upload_driver.getUrl(full_path);
        if (this.is_attachment) {
            //将数据写入附件表中
            const data = [
                name: file_name,
                dir: dir,
                att_type: type,
                real_name: file_info.name,//附件原名可能过长
                att_size: file_info.size,
                storage_type: self.storage_type,
                path: full_path,
                url: url,
                cate_id: cate_id,
            ];
            const att_id = core_attachment_service.add(site_id, data);
  }

  /**
   * after
   * 对应 PHP: CoreUploadService_core::after()
   * 逻辑类型: undefined - undefined
   */
  async after(site_id: number, file_dir: number, type: number, cate_id: number) {
    // 基于PHP真实逻辑: after
    // PHP原文: $file_info = $this->upload_driver->getFileInfo();         $dir = $this->root_path . '/' . $file_dir;         //执行上传         $this->upload_driver->setT...
const file_info = this.upload_driver.getFileInfo(];
        const dir = this.root_path  + '/' . file_dir;
        //执行上传
        this.upload_driver.setType(type).setValidate(this.validate).upload(dir];
        const file_name = this.upload_driver.getFileName(];
        const full_path = this.upload_driver.getFullPath(dir];
        const core_attachment_service = this.coreAttachmentService;
        const url = this.upload_driver.getUrl(full_path);
        if (this.is_attachment) {
            //将数据写入附件表中
            const data = [
                name: file_name,
                dir: dir,
                att_type: type,
                real_name: file_info.name,//附件原名可能过长
                att_size: file_info.size,
                storage_type: self.storage_type,
                path: full_path,
                url: url,
                cate_id: cate_id,
            ];
            const att_id = core_attachment_service.add(site_id, data);
  }

  /**
   * video
   * 对应 PHP: CoreUploadService_core::video()
   * 逻辑类型: undefined - undefined
   */
  async video(file: number, site_id: number, file_dir: number, cate_id: number, storage_type: number) {
    // 基于PHP真实逻辑: video
    // PHP原文: //实例化上传引擎         $this->upload_driver = $this->driver($site_id, $storage_type);         //读取上传附件的信息用于后续得校验和数据写入         $this->upload_driver->read($f...
//实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, FileDict.VIDEO, cate_id);
    }

    /**
     * 音频上传
     * @param string file
     * @param int site_id
     * @param string file_dir
     * @param int cate_id
     * @param string storage_type
     * @return * @throws BusinessException
     */
    async audio(string file, int site_id, string file_dir, int cate_id, string storage_type = '')
    {
        //实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, FileDict.AUDIO, cate_id);
    }

    /**
     * 上传文件
     * @param string file
     * @param int site_id
     * @param string type
     * @param string file_dir
     * @param string storage_type
     * @return * @throws BusinessException
     */
    async document(string file, int site_id, string type, string file_dir, string storage_type)
    {
        //实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, type);
    }

}
  }

  /**
   * audio
   * 对应 PHP: CoreUploadService_core::audio()
   * 逻辑类型: undefined - undefined
   */
  async audio(file: number, site_id: number, file_dir: number, cate_id: number, storage_type: number) {
    // 基于PHP真实逻辑: audio
    // PHP原文: //实例化上传引擎         $this->upload_driver = $this->driver($site_id, $storage_type);         //读取上传附件的信息用于后续得校验和数据写入         $this->upload_driver->read($f...
//实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, FileDict.AUDIO, cate_id);
    }

    /**
     * 上传文件
     * @param string file
     * @param int site_id
     * @param string type
     * @param string file_dir
     * @param string storage_type
     * @return * @throws BusinessException
     */
    async document(string file, int site_id, string type, string file_dir, string storage_type)
    {
        //实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, type);
    }

}
  }

  /**
   * document
   * 对应 PHP: CoreUploadService_core::document()
   * 逻辑类型: undefined - undefined
   */
  async document(file: number, site_id: number, type: number, file_dir: number, storage_type: number) {
    // 基于PHP真实逻辑: document
    // PHP原文: //实例化上传引擎         $this->upload_driver = $this->driver($site_id, $storage_type);         //读取上传附件的信息用于后续得校验和数据写入         $this->upload_driver->read($f...
//实例化上传引擎
        this.upload_driver = this.driver(site_id, storage_type];
        //读取上传附件的信息用于后续得校验和数据写入
        this.upload_driver.read(file];
        return this.after(site_id, file_dir, type);
    }

}
  }
}
